<?php

session_start();

require_once './db/arena-park-database.php';

require_once './doc/generate_doc.php';

require_once './email/MailMannagament.php';

$dataset = new ArenaParkDatabase();

if (isset($_REQUEST['type'])) {
	if ($_REQUEST['type'] == 'admin-loguin') {
		$_SESSION['admin'] = true;
		echo 1;
	} else if ($_REQUEST['type'] == 'logout') {
		unset($_SESSION['admin']);
		session_destroy();
		echo 1;
	} else if ($_REQUEST['type'] == 'insertE') {
		echo $dataset->setEnterprise($_REQUEST['enterprise']);
		
	}
	else if ($_REQUEST['type'] == 'findUser') {
		
//			echo $dataset->findUser($_REQUEST['text'], $_REQUEST['field']);
			$_SESSION['text'] = $_REQUEST['text'];
			$_SESSION['field'] = $_REQUEST['field'];
			echo 1;
	}else if ($_REQUEST['type'] == 'deleteUser') {
		
			echo $dataset->deleteUser($_REQUEST['userid']);
			
	}
	else if ($_REQUEST['type'] == 'paginar') {
		$echo = 0;
		if (!isset($_SESSION['paginar']))
			$_SESSION['paginar'] = 20;

		if ($_REQUEST['mode'] == 'start') {

			$_SESSION['from'] = 0;
			$_SESSION['to'] = $_SESSION['paginar'];
			$echo++;
		} else if ($_REQUEST['mode'] == 'end') {

			$totalRecords = $dataset->countRecords('user');

			$_SESSION['from'] = $totalRecords - $_SESSION['paginar'];
			$_SESSION['to'] = $totalRecords;
			$echo++;
		} else if ($_REQUEST['mode'] == 'prev') {

			if (($_SESSION['from'] - $_SESSION['paginar']) < 0) {

				$_SESSION['from'] = 0;
				$_SESSION['to'] = $_SESSION['paginar'];
			} else {
				$_SESSION['from'] -= $_SESSION['paginar'];
				$_SESSION['to'] -= $_SESSION['paginar'];
			}
			$echo++;
		} else if ($_REQUEST['mode'] == 'next') {

			$totalRecords = $dataset->countRecords('user');

			if ($totalRecords > $_SESSION['to'] && ($_SESSION['to'] + $_SESSION['paginar']) > $totalRecords) {

				$_SESSION['from'] = $_SESSION['to'];
				$_SESSION['to'] = $totalRecords;
				$echo++;
			} else if ($totalRecords > $_SESSION['to']) {
				$_SESSION['from'] += $_SESSION['paginar'];
				$_SESSION['to'] += $_SESSION['paginar'];
				$echo++;
			}
			echo $echo;
		}
		echo $_SESSION['from'] . '->' . $_SESSION['to'] . '->' . $_SESSION['paginar'];
	} else if ($_REQUEST['type'] == 'setPaginar') {
		if ($_REQUEST['number'] == 'todos')
			$_SESSION['paginar'] = $dataset->countRecords('user');
		else
			$_SESSION['paginar'] = $_REQUEST['number'];
		$_SESSION['from'] = 0;
		$_SESSION['to'] = $_SESSION['paginar'];
		echo 1;
	}
}
?>
